package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Room;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.mapper.RoomTypeMapper;
import com.zzyl.service.BedService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.zzyl.enums.BasicEnum.BED_INSERT_DUPLICATION_FAIL;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void createBed(BedDto bedDto) {

        Bed bedDb = bedMapper.selectByBedNumber(bedDto.getBedNumber());
        if (bedDb!= null) {
            throw new BaseException(BasicEnum.BED_INSERT_DUPLICATION_FAIL);
        }
        Bed bed = new Bed();
        BeanUtil.copyProperties(bedDto, bed);
        bed.setBedStatus(0);
        bedMapper.createdBed(bed);
        Room room = roomMapper.selectById(bedDto.getRoomId());
        roomTypeMapper.add(room.getTypeName());

    }

    @Override
    public BedVo getById(Long id) {
        Bed bed = bedMapper.getById(id);
        if (bed == null) {
            throw new NullPointerException("不存在这个房间");
        }
        BedVo bedVo = BeanUtil.toBean(bed, BedVo.class);
        return bedVo;
    }

    @Override
    public void updateBed(BedDto bedDto) {

        Bed byId = bedMapper.getById(bedDto.getId());
        if (byId.getBedStatus() != 0) {
            throw new BaseException(BasicEnum.BED_STATUS_ERROR);
        }
        Long count = bedMapper.countDiffBedNumber(bedDto.getBedNumber(), bedDto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_DUPLICATION_FAIL);
        }
      /*  Long id = bedDto.getId();
        BedVo bedVo = getById(id);
        bedVo.setSort(bedDto.getSort());
        bedVo.setBedNumber(bedDto.getBedNumber());
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())) {
            bedVo.setBedStatus(bedDto.getBedStatus());
        }*/
       /* Bed bed = BeanUtil.toBean(bedDto, Bed.class);*/
       /* bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);*/
        Bed bed=new Bed();
        BeanUtil.copyProperties(bedDto,bed);
        bedMapper.updateBed(bed);
    }

    @Override
    public void deleteById(Long id) {
        Bed bed = bedMapper.getById(id);
        if (bed!=null){
            bedMapper.deleteById(bed.getId());
            Room room = roomMapper.selectById(bed.getRoomId());
            roomTypeMapper.minus(room.getTypeName());
        }else {
            throw new BaseException(BasicEnum.BED_NOT_EXIST_ERROR);
        }





    }

}

